package opencl.impl;

import static gdts.common.logger.SimpleLogger.log;
import static opencl.utils.KernelUtils.readKernelFrom;

import java.io.IOException;

import opencl.IKernelCompiler;

import com.nativelibs4java.opencl.CLContext;
import com.nativelibs4java.opencl.CLKernel;
import com.nativelibs4java.opencl.CLProgram;
import com.nativelibs4java.opencl.JavaCL;

public class JavaCLKernelCompiler implements IKernelCompiler<CLKernel> {

    private CLContext context = JavaCL.createBestContext();
    private CLProgram program;

    @Override
    public CLKernel compile(String kernelDataFile) {
        String kernelString = null;
        try {
            kernelString = readKernelFrom(kernelDataFile);
        } catch (IOException e) {
            log("kernel " + kernelDataFile + " was not found!");
        }
        program = context.createProgram(kernelString).build();

        CLKernel kernel = program.createKernel(kernelDataFile.toString());
        if (kernel == null) {
            log("kernel " + kernelDataFile + " was not compiled");
        }
        return kernel;
    }
}